package com.software.loginregister.adapter;

import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.Post;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class HotPostAdapter extends RecyclerView.Adapter<HotPostAdapter.ViewHolder> {
    private List<Post> hotPosts = new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_hot, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Post post = hotPosts.get(position);

        // Set rank number and style
        holder.tvPostHotRank.setText(String.valueOf(position + 1));
        if (position < 3) {
            // Top 3 posts get special background colors
            int[] colors = {0xFFFFD700, 0xFFC0C0C0, 0xFFCD7F32}; // Gold, Silver, Bronze
            holder.tvPostHotRank.setBackgroundTintList(ColorStateList.valueOf(colors[position]));
        }

        // Set user info
        holder.tvPostHotUsername.setText(post.getNickName());
        holder.tvPostHotTime.setText(formatTime(post.getCreateTime()));

        // Set post content
        holder.tvPostHotContent.setText(post.getPostText());

        // Set interaction counts with icons
        holder.tvPostHotLikeCount.setText(String.valueOf(post.getLikeCount()));
        holder.tvPostHotLikeCount.setCompoundDrawablesWithIntrinsicBounds(
            R.drawable.ic_like_small, 0, 0, 0);

        holder.tvPostHotCommentCount.setText(String.valueOf(post.getCommentCount()));
        holder.tvPostHotCommentCount.setCompoundDrawablesWithIntrinsicBounds(
            R.drawable.ic_comment, 0, 0, 0);

    }


    private String formatTime(Date date) {
        if (date == null) {
            return "";
        }
        long diff = System.currentTimeMillis() - date.getTime();
        long hours = diff / (1000 * 60 * 60);
        if (hours < 24) {
            return hours + "小时前";
        } else if (hours < 48) {
            return "昨天";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd", Locale.getDefault());
            return sdf.format(date);
        }
    }

    @Override
    public int getItemCount() {
        return hotPosts.size();
    }

    public void setData(List<Post> posts) {
        this.hotPosts = posts;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvPostHotRank;
        ImageView ivPostHotAvatar;
        TextView tvPostHotUsername, tvPostHotTime, tvPostHotContent;
        TextView tvPostHotLikeCount, tvPostHotCommentCount;
        TextView tvPostTag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPostHotRank = itemView.findViewById(R.id.tv_post_hot_rank);
            ivPostHotAvatar = itemView.findViewById(R.id.iv_post_hot_avatar);
            tvPostHotUsername = itemView.findViewById(R.id.tv_post_hot_username);
            tvPostHotTime = itemView.findViewById(R.id.tv_post_hot_time);
            tvPostHotContent = itemView.findViewById(R.id.tv_post_hot_content);
            tvPostHotLikeCount = itemView.findViewById(R.id.tv_post_hot_like_count);
            tvPostHotCommentCount = itemView.findViewById(R.id.tv_post_hot_comment_count);
//            tvPostTag = itemView.findViewById(R.id.tv_post_tag);
        }
    }
}